package lanou.autohome.find;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

import java.util.ArrayList;

import lanou.autohome.R;
import lanou.autohome.SouSuoActivity;
import lanou.autohome.URLValues;
import lanou.autohome.basetool.BaseFragment;
import lanou.autohome.basetool.NetTool;
import lanou.autohome.carouselpic.FindCarouselAdapter;
import lanou.autohome.carouselpic.FindCarouselBean;
import lanou.autohome.find.adapters.FindAdapterForMe;
import lanou.autohome.find.adapters.FindAdapterTable;
import lanou.autohome.find.adapters.FindFenLeiAdapter;
import lanou.autohome.find.beans.FindFenLeiBean;
import lanou.autohome.find.beans.FindXianShiBen;

/**
 * Created by dllo on 16/8/15.
 */
public class FindFragment extends BaseFragment {

    private ArrayList<String> strings;
    private ImageButton imageButton;
    private ViewPager viewpager;
    private Handler handler;
    private RecyclerView recyclerView;
    private GridLayoutManager gridLayoutManager;
    private RecyclerView recyclerView1;
    private GridLayoutManager gridLayoutManager1;
    private RecyclerView recyclerView2;
    private GridLayoutManager gridLayoutManager2;
    private RecyclerView recyclerView3;
    private GridLayoutManager gridLayoutManager3;

    @Override
    protected int setLayout() {
        return R.layout.fragment_find;
    }

    @Override
    protected void initView() {
        viewpager = bindVie(R.id.viewpager_find);

        //搜索imagebutton
        imageButton = bindVie(R.id.btn_sousuo);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_sousuo:
                        Intent intent = new Intent(getContext(), SouSuoActivity.class);
                        startActivity(intent);
                }
            }
        });

        //限时抢购6张图片
        final ImageView imageView1 = bindVie(R.id.imageview_find_one);
        final ImageView imageView2 = bindVie(R.id.imageview_find_two);
        final ImageView imageView3 = bindVie(R.id.imageview_find_three);
        final ImageView imageView4 = bindVie(R.id.imageview_find_four);
        final ImageView imageView5 = bindVie(R.id.imageview_find_five);
        final ImageView imageView6 = bindVie(R.id.imageview_find_six);
        mNetTool.getNetData(URLValues.LIMITEDBUY_FIND_URL, FindXianShiBen.class, new NetTool.NetListener<FindXianShiBen>() {
            @Override
            public void onSuccess(FindXianShiBen findXianShiBen) {
                try {
                    mNetTool.getImage(findXianShiBen.getResult().getLimitedbuy().getLimitedbuyinfo().get(0).getImgurl(), imageView1);
                    mNetTool.getImage(findXianShiBen.getResult().getLimitedbuy().getLimitedbuyinfo().get(1).getImgurl(), imageView2);
                    mNetTool.getImage(findXianShiBen.getResult().getLimitedbuy().getLimitedbuyinfo().get(2).getImgurl(), imageView3);
                    mNetTool.getImage(findXianShiBen.getResult().getLimitedbuy().getLimitedbuyinfo().get(3).getImgurl(), imageView4);
                    mNetTool.getImage(findXianShiBen.getResult().getLimitedbuy().getLimitedbuyinfo().get(4).getImgurl(), imageView5);
                    mNetTool.getImage(findXianShiBen.getResult().getLimitedbuy().getLimitedbuyinfo().get(5).getImgurl(), imageView6);
                } catch (Exception e) {

                }
            }

            @Override
            public void onError(String errorMsg) {

            }
        });

        //活动专区的3张图片 和 它下边的两张横图
        final ImageButton imageButton1 = bindVie(R.id.imagebtn1_huodong_find);
        final ImageButton imageButton2 = bindVie(R.id.imagebtn2_huodong_find);
        final ImageButton imageButton3 = bindVie(R.id.imagebtn3_huodong_find);
        final ImageButton imagebtn1 = bindVie(R.id.imagebtn1_two_find);
        final ImageButton imagebtn2 = bindVie(R.id.imagebtn2_two_find);
        mNetTool.getNetData(URLValues.FUNCTION_FIND_URL, FindFenLeiBean.class, new NetTool.NetListener<FindFenLeiBean>() {
            @Override
            public void onSuccess(FindFenLeiBean findFenLeiBean) {
                mNetTool.getImage(findFenLeiBean.getResult().getImageads().getImageadsinfo().get(0).getImgurl(), imageButton1);
                mNetTool.getImage(findFenLeiBean.getResult().getImageads().getImageadsinfo().get(1).getImgurl(), imageButton2);
                mNetTool.getImage(findFenLeiBean.getResult().getImageads().getImageadsinfo().get(2).getImgurl(), imageButton3);
                mNetTool.getImage(findFenLeiBean.getResult().getActivitylist().get(0).getImgurl(), imagebtn1);
                try {

                    mNetTool.getImage(findFenLeiBean.getResult().getActivitylist().get(1).getImgurl(), imagebtn2);
                } catch (Exception e) {

                }
            }

            @Override
            public void onError(String errorMsg) {

            }
        });


        //猜你喜欢
        recyclerView = bindVie(R.id.recyclerview_find);
        gridLayoutManager = new GridLayoutManager(getContext(), 2);

        //为我推荐
        recyclerView1 = bindVie(R.id.recyclerview_find_forme);
        gridLayoutManager1 = new GridLayoutManager(getContext(), 2);

        //商品列表
        recyclerView2 = bindVie(R.id.recyclerview_find_table);
        gridLayoutManager2 = new GridLayoutManager(getContext(), 1);

        //10个分类 , 轮播图 和 限时抢购之间
        recyclerView3 = bindVie(R.id.recyclerview_find_functionlist);
        gridLayoutManager3 = new GridLayoutManager(getContext(), 5);
    }

    @Override
    protected void intiData() {

        //10个分类 轮播图 和 限时抢购之间
        final FindFenLeiAdapter findFenLeiAdapter = new FindFenLeiAdapter(getContext());
        mNetTool.getNetData(URLValues.FUNCTION_FIND_URL, FindFenLeiBean.class, new NetTool.NetListener<FindFenLeiBean>() {
            @Override
            public void onSuccess(FindFenLeiBean findFenLeiBean) {
                findFenLeiAdapter.setFindFenLeiBean(findFenLeiBean);
                recyclerView3.setLayoutManager(gridLayoutManager3);
                recyclerView3.setAdapter(findFenLeiAdapter);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });

        //猜你喜欢 , 为我推荐 , 商品列表
        final FindAdapter findAdapter = new FindAdapter(getContext());
        final FindAdapterForMe findAdapterForMe = new FindAdapterForMe(getContext());
        final FindAdapterTable findAdapterTable = new FindAdapterTable(getContext());
        mNetTool.getNetData(URLValues.FIND_URL, FindBean.class, new NetTool.NetListener<FindBean>() {
            @Override
            public void onSuccess(FindBean findBean) {
                findAdapter.setFindBean(findBean);
                recyclerView.setLayoutManager(gridLayoutManager);
                recyclerView.setAdapter(findAdapter);

                findAdapterForMe.setFindBean(findBean);
                recyclerView1.setLayoutManager(gridLayoutManager1);
                recyclerView1.setAdapter(findAdapterForMe);

                findAdapterTable.setFindBean(findBean);
                recyclerView2.setLayoutManager(gridLayoutManager2);
                recyclerView2.setAdapter(findAdapterTable);
            }

            @Override
            public void onError(String errorMsg) {
            }
        });

        //轮播图
        final FindCarouselAdapter findcarousadapter = new FindCarouselAdapter(getContext());
        strings = new ArrayList<>();
        mNetTool.getNetData(URLValues.CAROUSE_FIND_URL, FindCarouselBean.class, new NetTool.NetListener<FindCarouselBean>() {
            @Override
            public void onSuccess(FindCarouselBean findCarouselBean) {
                for (int i = 0; i < findCarouselBean.getResult().getList().size(); i++) {
                    strings.add(findCarouselBean.getResult().getList().get(i).getImgurl());

                }
                findcarousadapter.setStrings(strings);
                Log.d("FindFragment", "strings:" + strings);

            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        findcarousadapter.setViewPager(viewpager);
        viewpager.setAdapter(findcarousadapter);
        viewpager.setCurrentItem(3000, false);


        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(4000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    handler.sendEmptyMessage(viewpager.getCurrentItem());
                }
            }
        }).start();

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {

                int current = msg.what;
                viewpager.setCurrentItem(current + 1);
                return false;
            }
        });
    }
}
